import urllib

from adlist.models import SouhuModel,ToutiaoModel,ToutiaoPatameter
from django.http import HttpResponse, JsonResponse
from django.forms.models import model_to_dict
import json

from django.db.models.functions.base import Func
from django.views.decorators.csrf import csrf_exempt
from celery import task
import logging
from adlist.models import ToutiaoModel, RadarModel, SinaModel, SouhuModel, FenghuangModel, FeedsradarModel, \
    FeedsradarParameter, ToutiaoPatameter, SinaPatameter, RadarPatameter, SouhuPatameter, FenghuangPatameter
from adlist.controller import toutiao_object, fenghuang_object, sina_object, souhu_object, feedsradar_object, \
    radar_object
from adlist.tasks import fenghuang_create,radar_create,sina_create,toutiao_create,souhu_create,feedsradar_create
import random
from ad_manage.models import Advertisement, RandomData

class CustomeFunc(Func):
    function = 'DATE_FORMAT'
    template = '%(function)s(%(expressions)s,%(format)s)'

    def __init__(self, expression, **extra):
        expressions = [expression]
        super(CustomeFunc, self).__init__(*expressions, **extra)


from extra_apps.IMAGE_QUERY.extract import extract_feat

def testsouhu(request):

    img_pa = 'C:/Users/Administrator/Desktop/test.jpg'
    feat = extract_feat(img_pa
        )
    img_feat = feat.tolist()
    print(img_feat)

    print("end")

# 一次性执行方法，将所有local_img重新更新  #
from ad_manage.models import Advertisement
def update_all_localimg(request):
    ad = Advertisement.objects.filter(model_from='souhu_app').filter(title__icontains='最后')
    print(ad.count())
    for i in range(ad.count()):
        print(str(i)+":"+ad[i].title)

    return JsonResponse({"ad_date":"success"})

@csrf_exempt
def fenghuang_task(request):
    logger = logging.getLogger('django')
    logger.info("fenghuang_task is starting")
    Advertisement.objects.filter(model_from='fenghuang_app').delete()
    fenghuangs = FenghuangModel.objects.all()[17000:]
    fenghuang_create(fenghuangs)
    logger.info("tasks is ended")
    return JsonResponse({"json_data": "success"})


@csrf_exempt
def toutiao_task(request):
    logger = logging.getLogger('django')
    logger.info("toutiao_task is starting")
    Advertisement.objects.filter(model_from='toutiao_app').delete()
    toutiaos = ToutiaoModel.objects.filter(local_img__isnull=False)[0:10]
    toutiao_create(toutiaos)
    logger.info("tasks is ended")

    return JsonResponse({"json_data": "success"})
@csrf_exempt

def feedsradar_task(request):
    logger = logging.getLogger('django')
    logger.info("feedsradar_task is starting")
    Advertisement.objects.filter(model_from='feedsradar_app').delete()
    feedsradarModel = FeedsradarModel.objects.all()
    feedsradar_create(feedsradarModel)
    logger.info("tasks is ended")
    return JsonResponse({'json_data': 'success'})

@csrf_exempt

def radar_task(request):
    logger = logging.getLogger('django')
    logger.info("radar_tasks is starting")
    Advertisement.objects.filter(model_from='radar_app').delete()
    radars = RadarModel.objects.all()
    radar_create(radars)
    logger.info("tasks is ended")



@csrf_exempt
def sina_task(request):
    logger = logging.getLogger('django')
    logger.info("sina_task is starting")
    Advertisement.objects.filter(model_from='sina_app').delete()
    sinas = SinaModel.objects.all()
    sina_create(sinas)
    logger.info("tasks is ended")
    return JsonResponse({'json_data': 'success'})

@csrf_exempt
def souhu_task(request):
    logger = logging.getLogger('django')
    logger.info("souhu_task is starting")
    Advertisement.objects.filter(model_from='souhu_app').delete()
    souhus = SouhuModel.objects.all()
    souhu_create(souhus)
    logger.info("tasks is ended")
    return JsonResponse({'json_data': 'success'})



